package com.gvtv.main.hive;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import com.gvtv.main.exception.ExecuteException;
import com.gvtv.main.util.PropertiesHelper;

public class HiveJdbc {

	private Connection conn = null;
	private Statement st;
	private PreparedStatement pst;
	private ResultSet rs;
	
	public Connection getConnection() throws ClassNotFoundException, SQLException {
		if (conn == null) {
			try {
				Class.forName(PropertiesHelper.getString("hive.driver"));
				conn = DriverManager.getConnection(PropertiesHelper.getString("hive.url"));
			} catch (ClassNotFoundException e) {
				throw e;
			} catch (SQLException e) {
				throw e;
			}
		}
		return conn;
	}
	
	public void execute(String sql) throws ExecuteException{
		Statement statement = null;
		try {
			statement = getConnection().createStatement();
			statement.executeQuery(sql);
		} catch (SQLException e) {
			throw new ExecuteException(e);
		} catch (ClassNotFoundException e) {
			throw new ExecuteException(e);
		} finally {
			close(statement);
		}
	}
	
	public void execute(String sql, String... args) throws ExecuteException{
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = getConnection().prepareStatement(sql);
			setValue(preparedStatement, args);
			preparedStatement.executeQuery();
		} catch (SQLException e) {
			throw new ExecuteException(e);
		} catch (ClassNotFoundException e) {
			throw new ExecuteException(e);
		} finally {
			close(preparedStatement);
		}
	}
	
	public ResultSet query(String sql) throws ExecuteException{
		try {
			st = getConnection().createStatement();
			rs = st.executeQuery(sql);
		} catch (SQLException e) {
			throw new ExecuteException(e);
		} catch (ClassNotFoundException e) {
			throw new ExecuteException(e);
		}
		return rs;
	}
	
	public ResultSet query(String sql, String... args) throws ExecuteException{
		try {
			pst = getConnection().prepareStatement(sql);
			setValue(pst, args);
			rs = pst.executeQuery();
		} catch (SQLException e) {
			throw new ExecuteException(e);
		} catch (ClassNotFoundException e) {
			throw new ExecuteException(e);
		}
		return rs;
	}

	public List<Map<String, Object>> queryForList(String sql){
		
		return null;
	}
	
	public List<Map<String, Object>> queryForList(String sql, String... args){
		
		return null;
	}
	
	private void setValue(PreparedStatement pst, String... values) throws SQLException {
		if (values == null) {
			return;
		}
		try {
			for (int i = 0; i < values.length; i++) {
				pst.setString(i + 1, values[i]);
			}
		} catch (SQLException e) {
			throw new SQLException(e);
		}
	}
	
	public void close(Object obj){
		if(obj==null){
			return;
		}
		try {
			if(obj instanceof ResultSet){
				((ResultSet) obj).close();
			}
			if(obj instanceof Statement){
				((Statement) obj).close();
			}
			if(obj instanceof PreparedStatement){
				((PreparedStatement) obj).close();
			}
			if(obj instanceof Connection){
				((Connection) obj).close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void close(){
		close(rs);
		close(pst);
		close(st);
		close(conn);
	}
	public static void main(String[] args) {
		HiveJdbc hiveJdbc = new HiveJdbc();
		
		System.out.println("------------");
	}

}
